<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 小程序登录入口
 * @author liubin-bt
 * @property User_model $User_model
 */
class Login extends CI_Controller {
    
    public function __construct(){
        parent::__construct();
    }
    
    /**
     * 微信登录
     */    
   public function wxLogin(){
       $this->config->load('weixinconfig');
       $appid = $this->config->item('appid');;
       $appsecret = $this->config->item('secret');
       $code = isset($_POST['code']) ? trim($_POST['code']) : '';
       if (!$code) {
           echo json_encode(array('status'=>-1,'msg'=>'非法操作！'));
           exit();
       }
       if (!$appid || !$appsecret) {
           echo json_encode(array('status'=>-1,'msg'=>'非法操作！'));
           exit();
       } 
       //获取session值
       $url = "https://api.weixin.qq.com/sns/jscode2session?appid=".$appid."&secret=".$appsecret."&js_code=".$code."&grant_type=authorization_code";
       $res = json_decode($this->httpGet($url),true);
       if( empty($res['session_key']) ){
           echo json_encode(array('status'=>-1,'msg'=>'获取微信登录信息失败！'));
           exit;
       }
       $sessionKey = isset($res['session_key']) ? $res['session_key'] : '';//取出json里对应的值
       $encryptedData = isset($_POST['encryptedData']) ? $_POST['encryptedData'] : '';
       $iv = isset($_POST['iv']) ? $_POST['iv'] : '';
       //解码小程序传过来的用户信息
       include_once APPPATH."libraries/wxBizDataCrypt.php";
       $pc = new WXBizDataCrypt($appid, $sessionKey);
       $errCode = $pc->decryptData($encryptedData, $iv, $data);
       if ($errCode == 0) {
           $this->load->model('User_model');
           //开始登陆
           $wxuserinfo = json_decode($data, true);
           $user['unionid'] = $wxuserinfo['unionId'];   //pc端和小程序端用户打通标识
           $user['openid'] = $wxuserinfo['openId'];
           $reguser = $this->User_model->getUserInfo(['unionid'=>$user['unionid']]);
           if( $reguser ){
               $reguser['authinfo'] = null;
               $authinfo = $this->User_model->getAuthInfo($reguser['id']);
               if( !empty($authinfo) ){
                   $reguser['authinfo'] = $authinfo['status'] == 1 ? $authinfo : null;
               }
               $reguser['uid'] = $reguser['id'];
               unset($reguser['id']);
               $returndata = $reguser;
           } else {
               $user['user_name'] = $wxuserinfo['nickName'];
               $user['photo'] = $wxuserinfo['avatarUrl'];
               $user['corp_id'] = 0;
               $user['group_id'] = 0;
               $user['ctime'] = time();
               $user['regfrom'] = 1;
               $uid = $this->User_model->insert($user);
               if( $uid ){
                   $user['uid'] = $uid;
               }
               $returndata = $user;
           }
           unset($returndata['unionid']);
           $this->session->user = $returndata;
           $returndata['cookiesession'] = 'hpg_session='.session_id();
           echo json_encode(['status'=>0, 'user'=>$returndata]);
           exit;
       } else {
           echo json_encode(['status'=>$errCode, 'msg'=>'微信数据解析错误']);
           print($errCode . "\n");
       }
    }
	 
    function httpGet($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL, $url);
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }
}
